﻿using Adp;
using Adp.Common;
using Adp.Core.Attribute;
using Microsoft.AspNetCore.Mvc;
using Yqd.Core;
using Yqd.Enm;
using Yqd.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yqd.Controllers.Filter;
using System.Threading;

namespace Yqd.Controllers
{
    public class IntegralRuleController : BaseController
    {
        YqdContext ctx;

        public IntegralRuleController(YqdContext ctx)
        {
            this.ctx = ctx;
        }

        //[Master]
        public ActionResult GetList()
        {
            var q = ctx.Yqd_IntegralRule.AsQueryable();
            var types = typeof(任务状态);
            if (!q.Any())
            {
                var lst = new List<Yqd_IntegralRule>();
                foreach (int item in Enum.GetValues(types))
                {
                    if (item >0)
                        lst.Add(new Yqd_IntegralRule()
                        {
                            IntegralRuleID = (任务状态)item,
                            Value = 0,
                            Topset = item,
                            Instime = DateTime.Now
                        });
                }
                if (lst.Any())
                {
                    ctx.Yqd_IntegralRule.AddRange(lst);
                    ctx.SaveChanges();
                }
            }
            return this.ToJson(new
            {
                success = true,
                totalcount = q.Count(),
                data = q.OrderBy(it => it.Topset).ToList().Select(m => new
                {
                    m.IntegralRuleID,
                    Name = Enum.GetName(types, m.IntegralRuleID),
                    m.Remark,
                    m.Value
                })
            });
        }

        public ActionResult Get()
        {
            var RuleID = (任务状态)Form.GetInt("RuleID");
            var m = ctx.Yqd_IntegralRule.FirstOrDefault(it => it.IntegralRuleID == RuleID);
            if (m != null)
            {
                return this.ToJson(new
                {
                    success = true,
                    data = new
                    {
                        m.IntegralRuleID,
                        m.Value,
                        m.Remark
                    }
                });
            }
            return this.ToJson(new
            {
                success = false,
                msg = "规则不存在"
            });
        }

        public ActionResult Edit(Yqd_IntegralRule model)
        {
            if (!ModelState.IsValid)
            {
                return this.ToJson(new
                {
                    success = false,
                    stateMsg = from sk in ModelState.AsEnumerable()
                               where sk.Value.Errors.Count() > 0
                               select new { id = sk.Key, msg = sk.Value.Errors.FirstOrDefault().ErrorMessage }
                });
            }
            try
            {
                var ret = 0;
                var m = ctx.Yqd_IntegralRule.FirstOrDefault(it => it.IntegralRuleID == model.IntegralRuleID);
                if (m != null)
                {
                    m.Value = model.Value;
                    m.Remark = model.Remark;
                    ret = ctx.SaveChanges();
                }
                return this.ToJson(new
                {
                    success = true,
                    ret
                });
            }
            catch
            {
                return this.ToJson(new
                {
                    success = false,
                    msg = "规则不存在",
                });
            }
        }

        public ActionResult UpdateIntegral()
        {
            Task.Factory.StartNew(() => RankService.UpdateUnitRank(), TaskCreationOptions.LongRunning);
            Thread.Sleep(3000);
            return this.ToJson(new
            {
                success = true
            });
        }

        public ActionResult UpdateTest()
        {
            Task.Factory.StartNew(() => RankService.UpQMDataToYiWu(), TaskCreationOptions.LongRunning);
            Thread.Sleep(3000);
            return this.ToJson(new
            {
                success = true
            });
        }

    }
}
